import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Point2D;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class MatrixInput extends LabelInput {
	private Arrow arrow;
	private Representation repr;
	private String[][] map;
	
	public MatrixInput(Representation repr, Arrow arrow) {
		this.repr = repr;
		this.arrow = arrow;
		map = repr.getMap(arrow);
		if(map.length==0 || map[0].length==0){
			return;
		}
		setModel(new DefaultTableModel(map.length, map[0].length));
		for (int i=0; i<map.length; i++){
			for(int j=0; j<map[0].length; j++){
				setValueAt(map[i][j], i, j);
			}
		}
		point = arrow.getLabelPoint();
		init();
	}


    @Override
    protected void addHideKeyListener() {
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent keyEvent) {

            }

            @Override
            public void keyPressed(KeyEvent keyEvent) {
            }

            @Override
            public void keyReleased(KeyEvent keyEvent) {
                switch(keyEvent.getKeyCode()) {
                    case KeyEvent.VK_ENTER:
                        if (getSelectedRow() == 0 && getSelectedColumn() == 0) {
                            write();
                            setVisible(false);
                        }
                        break;
                    case KeyEvent.VK_ESCAPE:
                        setVisible(false);
                        break;
                }
            }
        });
    }

    @Override


	protected void write(){
		if(map.length==0 || map[0].length==0){
			return;
		}
		for (int i=0; i<map.length; i++){
			for(int j=0; j<map[0].length; j++){
				map[i][j] = (String) getValueAt(i, j);
			}
		}
		repr.setMap(arrow, map);
	}
}
